﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;

using SBPweb.RIA.Client.Contracts.Base;

using SBPweb.PROvibe.Client.Library.Models;
using SBPweb.PROvibe.Shared.Library.Components;
using SBPweb.PROvibe.Shared.Library.Layout;

namespace SBPweb.PROvibe.Client.Core.Models
{

    public class ContentContextModel : ContentContextModelBase, IContentContextModel
    {

        #region Fields

        private ICollection<IModule> pendingModules = new Collection<IModule>();

        #endregion

        #region Dependencies

        #endregion

        #region Properties

        public new IApplication Application
        {
            get
            {
                return base.Application;
            }
            internal set
            {
                base.Application = value;
            }
        }

        public new ReadOnlyObservableCollection<IModule> Modules
        {
            get
            {
                return base.Modules;
            }
            internal set
            {
                base.Modules = value;
            }
        }

        public new IPage Page
        {
            get
            {
                return base.Page;
            }
            internal set
            {
                base.Page = value;
            }
        }

        public new ReadOnlyObservableCollection<IService> Services
        {
            get
            {
                return base.Services;
            }
            internal set
            {
                base.Services = value;
            }
        }

        public new LayoutModes Layout
        {
            get
            {
                return base.Layout;
            }
            internal set
            {
                base.Layout = value;
            }
        }

        #endregion

        #region Events

        public event EventHandler PendingModulesStarted;

        public event EventHandler PendingModulesFinished;

        #endregion

        #region Methods

        internal void EnqueuePendingModule(IModule module)
        {
            lock (pendingModules)
            {
                if (pendingModules.Count == 0 && PendingModulesStarted != null)
                {
                    PendingModulesStarted(this, EventArgs.Empty);
                }
                pendingModules.Add(module);
            }
        }

        internal void EnqueuePendingModules(IEnumerable<IModule> modules)
        {
            lock (pendingModules)
            {
                if (pendingModules.Count == 0 && PendingModulesStarted != null)
                {
                    PendingModulesStarted(this, EventArgs.Empty);
                }
                foreach (IModule module in modules)
                {
                    pendingModules.Add(module);
                }
            }
        }

        internal void DequeuePendingModule(IModule module)
        {
            lock (pendingModules)
            {
                pendingModules.Remove(module);
                if (pendingModules.Count == 0 && PendingModulesFinished != null)
                {
                    PendingModulesFinished(this, EventArgs.Empty);
                }
            }
        }
        
        #endregion

    }

}